﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data;
using System.ComponentModel;

namespace Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class saunaMain : Window
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer;

        public saunaMain()
        {
            InitializeComponent();
        }


        // USER MANAGEMENT

        private void btnEditProfile_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            dispatcherTimer.Start();

            formController.GetProfile(formController.user.username);
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            formController.Logout(formController.user.username);
            formController.ShowForm(typeof(mainWindow));
        }

        private void btnChat_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            dispatcherTimer.Start();

            User selectedUser = (User)dtgFriends.SelectedValue;

            if (selectedUser != null)
            {
                formController.GetProfile(selectedUser.username);
                //formController.editAccount(selectedUser);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            dispatcherTimer.Start();

            User selectedUser = (User)dtgFriends.SelectedValue;

            if (selectedUser != null)
            {
                if (MessageBox.Show("Are you sure you want to delete " + selectedUser.username + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    Console.WriteLine("Yes");

                    formController.DeleteUser(selectedUser.username);
                }
                else
                {
                    Console.WriteLine("No");
                }
            }
 
        }

        private void btnAboutSauna_Click(object sender, RoutedEventArgs e)
        {
            formController.ShowForm(typeof(AboutSauna));
        }
        


        // FILE MANAGEMENT

        private void btnAddFile_Click(object sender, RoutedEventArgs e)
        {

            dispatcherTimer.Stop();
            dispatcherTimer.Start();

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ".txt"; // Default file name
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "Text Document (.txt) |*.txt" + "|Program (.exe) |*.exe" + "|All Files |*.*"; // Filter files by extension
            //dlg.Filter += ".txt Files (.txt)|*.txt";

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                formController.xfile.xfilepath = dlg.FileName;
                String[] strFileNameArray = formController.xfile.xfilepath.Split('\\');
                formController.xfile.xfilename = strFileNameArray[strFileNameArray.Length - 1];

                formController.FileExists();
            }
        }

        private void btnDeleteFile_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            dispatcherTimer.Start();

            XFile selectedFile = (XFile)dtgSoftware.SelectedValue;

            if (selectedFile != null)
            {
                if (MessageBox.Show("Are you sure you want to delete " + selectedFile.xfilename + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    formController.DeleteFile(selectedFile.xfilename);
                }
                else
                {

                }
            }
        }


        // FORM MANAGEMENT

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (formController.newProtocol == true)
            {
                switch (formController.receivedProtocol)
                {

                    #region Get User List

                    case "GUL":
                        {
                            //MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
                            //mainWindowViewModel.displayUserList(formController.userlist);

                            //DataContext = mainWindowViewModel;
                            //dispatcherTimer.Stop();
                        }
                        break;

                    #endregion

                    #region Get Profile

                    case "GP":
                        {
                            dispatcherTimer.Stop();
                            formController.EditAccount();
                        }
                        break;

                    #endregion

                    #region User Deleted

                    case "UD":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                formController.GetUserList(formController.user.username);
                            }
                            else
                            {
                                dispatcherTimer.Stop();
                            }
                        }
                        break;

                    #endregion

                    #region Name File

                    case "NF":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                formController.ListFiles();
                            }
                            else
                            {
                                if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }
                                dispatcherTimer.Stop();
                            }
                        }
                        break;

                    #endregion

                    #region File Exists

                    case "FE":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }
                            }
                            else
                            {
                                formController.AddFile();
                            }
                        }
                        break;

                    #endregion

                    #region List Files

                    case "LF":
                        {
                            MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
                            mainWindowViewModel.displayXFileList(formController.xfilelist);
                            mainWindowViewModel.displayUserList(formController.userlist);

                            DataContext = mainWindowViewModel;
                            dispatcherTimer.Stop();
                        }
                        break;

                    #endregion

                    #region User Deleted

                    case "DF":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                formController.ListFiles();
                            }
                            else
                            {
                                if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }

                                dispatcherTimer.Stop();
                            }
                        }
                        break;

                    #endregion


                        
                    default:

                        break;
                }

                formController.newProtocol = false;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            dispatcherTimer.Start();
        }



    }
}
